﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using BugLister.Dialogs;
using Microsoft.Win32;

namespace BugLister
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        OpenFileDialog _open;
        SaveFileDialog _save;
        BugCollection _bugs;

        string _fileName;
        public MainWindow()
        {
            InitializeComponent();
            _bugs = new BugCollection();

            bugList.DataContext = (_bugs);
            _bugs.Add(new BugEntry
            {
                ShortDescription = "S",
                Importance = BugEntry.BugImportance.Critical,
                Fixed = true
            });
            var importanceLevelNames = new List<string>()
            {
                "None"
            };
            importanceLevelNames.AddRange(Enum.GetNames(typeof(BugEntry.BugImportance)));
            filterImportanceCombo.ItemsSource = importanceLevelNames;

            var typeNames = new List<string>()
            {
                "None"
            };
            typeNames.AddRange(Enum.GetNames(typeof(BugEntry.BugType)));
            filterTypeCombo.ItemsSource = typeNames;

            filterTypeCombo.SelectionChanged += FilterChanged;
            filterImportanceCombo.SelectionChanged += FilterChanged;
            isFixedFilterCheckBox.SelectionChanged += FilterChanged;

            _save = new SaveFileDialog();
            _open = new OpenFileDialog();
            _save.Filter = _open.Filter = "XML Files|*.xml";
        }

        
        public void NewBugListButton_Click(object sender, RoutedEventArgs e)
        {
            _bugs.Clear();
            _fileName = null;
        }

        public void ViewBugButton_Click(object sender, RoutedEventArgs e)
        {
            var bug = bugList.SelectedItem as BugEntry;
            if (bug == null) return;
            new ViewBugDialog(bug).ShowDialog();
        }

        public void EditBugButton_Click(object sender, RoutedEventArgs e)
        {
            var editDialog = new EditBugDialog();
            
            throw new NotImplementedException();
        }

        public void FilterChanged(object sender, SelectionChangedEventArgs e)
        {
            if (_bugs == null || filterImportanceCombo == null || filterTypeCombo == null)
                return;
            BugEntry.BugType type;
            BugEntry.BugImportance importance;
            if (Enum.TryParse(filterImportanceCombo.SelectedValue.ToString(), out importance))
            {
                _bugs.Filter(importance);
            }
            else
            {
                _bugs.Filter(bugImportance: null);
            }
            if (Enum.TryParse(filterTypeCombo.SelectedValue.ToString(), out type))
            {
                _bugs.Filter(type);
            }
            else
            {
                _bugs.Filter(bugType: null);
            }
        }
        private void AddBugButton_Click(object sender, RoutedEventArgs e)
        {
            AddBugDialog dialog = new AddBugDialog();
            var thisTop = this.Topmost;
            this.Topmost = false;
            if (dialog.ShowDialog() ?? false)
            {
                _bugs.Add(dialog.Bug);
            }
            this.Topmost = thisTop;
        }

        private void RemoveBugButton_Click(object sender, RoutedEventArgs e)
        {
            int idx = bugList.SelectedIndex;
            if (idx >= 0 && idx < _bugs.Count)
            {
                _bugs.RemoveAt(idx);
            }
        }

        private void SaveBtn_Click(object sender, RoutedEventArgs e)
        {
            if(_fileName != null)
                BugCollection.SaveBugs(_fileName, _bugs);
            else if (_save.ShowDialog() ?? false)
            {
                _fileName = _save.FileName;
                BugCollection.SaveBugs(_save.FileName, _bugs);
            }
        }

        private void LoadBtn_Click(object sender, RoutedEventArgs e)
        {
            if (_open.ShowDialog() ?? false)
            {
                try
                {
                    _bugs = BugCollection.LoadBugs(_open.FileName);
                    _fileName = _open.FileName;
                    bugList.DataContext = _bugs;
                }
                catch (Exception ex)
                {
                    MessageBox.Show("Could load selected file to the editor.\nFile is in the wrong format.");
                }
            }
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

        }

        private void bugList_MouseDoubleClick(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            var uiElement = (UIElement)bugList.InputHitTest(e.GetPosition(bugList));
            if (uiElement is ListBoxItem)
            {
                var bug = (uiElement as ListBoxItem).Content as BugEntry;
                new ViewBugDialog(bug).ShowDialog();
            }
        }
    }
}
